<?php
namespace Home\Controller;
use Org\Net;
use Think\Controller;
class ZhuanController extends Controller
{
	const CIPHER = MCRYPT_RIJNDAEL_128;
	const MODE = MCRYPT_MODE_CBC;
	private $key;
	
	public function __construct(){
	    $this->key = C('MD_PWD');
	}
	
	//定义空操作，避免错误反馈
	public function _empty(){ 
		//空操作
	}
	
	
	//米迪的加密算法
	private function encrptAesBase64($plaintext){
		$iv ="\x01\x02\x03\x04\x05\x06\x07\x08\x09\x00\x01\x02\x03\x04\x05\x06";
		$padding = 16-(strlen($plaintext)%16);
		$plaintext .=str_repeat(chr($padding),$padding);
		$cliphertext =mcrypt_encrypt(self::CIPHER,$this->key,$plaintext,self::MODE,$iv);
		$cliphertext = base64_encode($cliphertext);
		return urlencode($cliphertext);
	}
	
	//米迪的链接URL(未过虑参数)
	public function index(){
	    $openid=I("get.openid");
		$a = new \Home\Model\WeixinModel();
		$b = $a->getweixin();
		$model=M("client");
		$userinfo=$model->where(array("openid"=>$openid))->find();
		if(is_array($userinfo) ){
		    if($userinfo["status"]==0){//被拉黑用户
		       $b->text("你被拉黑了，无法赚取积分！")->reply();
		    }
		    else
		    {   
		        if(empty($userinfo["uname"])|| empty($userinfo["headimg"])){
		            $b->text("微信用户必须设置昵称和头像才能赚取积分")->reply();
		        }
		        else{
        		    $MyIp = new \Org\Net\IpLocation('UTFWry.dat'); // 实例化类 参数表示IP地址库文件
        		    $ip= get_client_ip();
        		    $area = $MyIp->getlocation($ip); // 获取某个IP地址所在的位置
        		    $userinfo["ip"]=$ip;
        		    $userinfo["area"]=$area['country'].$area['area'];
        		    $model->where(array("openid"=>$openid))->data($userinfo)->save();
        		    /**
        		     $location['ip'] // IP地址
        		     $location['beginip'] // 用户IP所在范围的开始地址
        		     $location['endip'] // 用户IP所在范围的结束地址
        		     $location['country'] // 所在国家或者地区
        		     $location['area'] // 所在区域
        		     */
        		    $miidi_wxPetName=$userinfo["uname"]; //关注公众号的微信用户的昵称，一定要是用户真  实的昵称
        		    $miidi_wxUserIcon=$userinfo["headimg"]; //关注公众号的微信用户的头
        		    
            		$miidi_appid=C("MD_YYID");//开发者在米迪后台注册的应用的：发布ID
            		$miidi_openId=$openid;//    "o5cINt6s85O6SK-ov6xyWuC0O0zY";//$openid;//微信用户的openid
            		$miidi_param0=$openid ;//"o5cINt6s85O6SK-ov6xyWuC0O0zY";//$openid;//开发者用来判断唯一用户的标准，可以用openId作为值，也可以是开发者自己定义的用户id，米迪回调开发者的时候会原样返回，开发者需要用这个值区分用户
            		$miidi_appname=C("MD_NAME");//"淘金币"; //开发者的公众号，也是开发者在米迪后台注册的应用的：应用名称
            	  //$miidi_encryptStr ="openId=".$miidi_openId."&appName=".$miidi_appname."&param0=".$miidi_param0;//三个参数缺一不可
            		$miidi_encryptStr="openId=".$miidi_openId."&appName=".$miidi_appname."&param0=".$miidi_param0.
            		"&wxPetName=".urlencode($miidi_wxPetName)."&wxUserIcon=".urlencode($miidi_wxUserIcon);
            		$miidi_encrypt_res =$miidi_appid."_".$this->encrptAesBase64($miidi_encryptStr);// appId+ "_"也必不可缺
            		//打开积分墙URL
            		$miidi_url_str ="http://ow.miidi.net/ow/wxList.bin?r=".$miidi_encrypt_res;
            		header("Location:".$miidi_url_str);	
		        }	
		   }
		}else{
		    $b->text("系统网络问题，请稍后再试")->reply();
		}
	}
	
	
	//接受回调数据
	public function getmessage(){
		if(IS_GET){
			$id = I('get.id','intval');
			$trand_no = I('get.trand_no','intval');
			$cash = I('get.cash','intval');
			$param0 = I('get.param0');
			$sign = I('get.sign');
			$key=C("MD_KEY");//"fsmc65m21wuirz4do4sij1fc1oeol6";
			$s = md5($id.$trand_no.$cash.$param0.$key);
			if($s==$sign){
				$appName = I('get.appName');
				$bundleId = I('get.bundleId');
				$scoreType = I('get.scoreType');
				$imei = I('get.imei');
				$Note = M('Note');
				if(!$Note->field('trand_no')->where("`trand_no`=$trand_no")->find()){
					$date =array();
					$date["id"]=$id;
					$date["trand_no"]=$trand_no;
					$date["cash"]=$cash;
					$date["userid"]=$param0;
					$date["scoreType"]=$scoreType;
					$date["appName"]=$appName;
					$date["bundleId"]=$bundleId;
					$date["imei"]=$imei;
					$date["ctime"]=time();
					$date["times"]=date("Y-m-d H:i:s",time());
					$Note->add($date);
					$user = M('client');
					$user->where("openid =".$param0)->setInc('score',$cash);
					$userinfo=$user->where("openid =".$param0)->find();
					if($userinfo)
					{
					    if($userinfo["shangxian"])
					    {
    					    $yaoqing=M("yaoqing");
    					    $array=array(
    					        "cid"=>$userinfo["uid"],					      
    					        "score"=>$cash * C("yaoqingbilv"),
    					        "times"=>date("Y-m-d H:i:s",time()),
    					        "openid"=>$param0,
    					        "shangxianopenid"=>$userinfo["shangxian"]
    					    ) ;
    					   $bool=$yaoqing->data($array)->add();
    					   $user->where("openid =".$userinfo["shangxian"])->setInc('score',$cash * C("yaoqingbilv"));
    					   if(!$bool){
    					        M("log")->data(array("info"=>"充值回调插入md_client用户积分失败","times"=>date("Y-m-d H:i:s",time()),"openid"=>$param0))->add();
    					   }
					     }
					}else{
					    M("log")->data(array("info"=>"充值回调获取md_client用户信息失败","times"=>date("Y-m-d H:i:s",time()),"openid"=>$param0))->add();
					}
				}else{
				    M("log")->data(array("info"=>"充值回调获取md_note信息失败","times"=>date("Y-m-d H:i:s",time()),"openid"=>$param0))->add();
				}
			}else{
				M("log")->data(array("info"=>"充值回调返回信息错误","times"=>date("Y-m-d H:i:s",time()),"openid"=>$param0))->add();
			}
		}
	}
}

?>